package com.wang.tool.util;

import java.util.concurrent.ThreadLocalRandom;

/**
 * @Author wangshaopeng
 * @Date 2022/7/3
 */
public class RandomUtil {
    private static final String BASE_STR = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEBGHIJKLMNOPQRSTUVWXYZ";

    private static final String ENG_STR = "abcdefghijklmnopqrstuvwxyz";


    /**
     * 生成随机英文大小写字母数字组成的字符串
     *
     * @param length
     * @return
     */
    public static String randomStr(int length) {
        StringBuilder sb = new StringBuilder(length);
        if (length < 1) {
            length = 1;
        }
        int baseLength = BASE_STR.length();
        for (int i = 0; i < length; ++i) {
            int number = ThreadLocalRandom.current().nextInt(baseLength);
            sb.append(BASE_STR.charAt(number));
        }
        return sb.toString();
    }


    /**
     * 生成随机英文字母组成的字符串
     *
     * @param length
     * @return
     */
    public static String randomEng(int length) {
        StringBuilder sb = new StringBuilder(length);
        if (length < 1) {
            length = 1;
        }
        int baseLength = ENG_STR.length();
        for (int i = 0; i < length; ++i) {
            int number = ThreadLocalRandom.current().nextInt(baseLength);
            sb.append(ENG_STR.charAt(number));
        }
        return sb.toString();
    }

}
